<?php

/*
*Plugin Name: Tunna Slider
*Plugin Author: wiloke 
*Plugin URI : http://themeelite.com
*Author URI: http://wiloke.com
*Version: 1.0
*Description: Easy create a slider. This coming with qva theme.
*/


define('TN_ADMIN_SLIDER_IMAGE', plugin_dir_url(__FILE__) . 'assets/images/');
define('TN_ADMIN_SLIDER_JS', plugin_dir_url(__FILE__) . 'assets/js/');
define('TN_ADMIN_SLIDER_CSS', plugin_dir_url(__FILE__) . 'assets/css/');


class woSlider
{
	const LANG = 'tunna_slider';
	public $aOptions;
	public function __construct()
	{
		// add_action('init', array($this, "getOptions"));
		// $this->getOptions();

		// add_action('admin_menu', array($this, 'wo_hn_slider'));
		add_action('init', array($this, 'tn_register_post_type'));
		// add_action('admin_menu', array($this, 'wo_add_menu'));
		add_action('edit_form_after_title', array($this, 'tn_tunnar_slider_settings'));
		add_action('wp_ajax_add-slider', array($this, 'tn_create_form_slide'));
		add_action('save_post', array($this, 'tn_save_tunnar_slider'), 10, 2);
		add_action('add_meta_boxes', array($this, 'tn_metabox_additional'));

		if ( is_admin() )
		{
			add_action('admin_enqueue_scripts', array($this, 'tn_slider_include_scripts'));
		}


		add_shortcode('tunna_slider', array($this, 'wo_tunna_shortcode'));
		

	}	

	public function getOptions()
	{	
		global $post;
		return $this->aOptions=get_post_meta($post->ID, "tunna_slider", true);
	}

	public function tn_metabox_additional()
	{
		
		// $this->getOptions();


		add_meta_box(
			'wo_effect',
			__( 'Effect', self::LANG ),
			array($this,'wo_slider_effect'),
			self::LANG,
			'side'
		);

		// add_meta_box(
		// 	'wo_pagination',
		// 	__( 'Pagination', self::LANG ),
		// 	array($this,'wo_slider_pagination'),
		// 	self::LANG,
		// 	'side'
		// );


		add_meta_box(
			'wo_color_schemes',
			__( 'Highlight Color', self::LANG ),
			array($this, 'wo_color_schemes_dashboard'),
			self::LANG,
			'side'
		);
		add_meta_box(
			'wo_select_pattern',
			__( 'Pattern', self::LANG ),
			array($this,'wo_pattern_dashboard'),
			self::LANG,
			'side'
		);

		add_meta_box(
			'wo_overlay',
			__( 'Overlay', self::LANG ),
			array($this,'wo_overlay_dashboard'),
			self::LANG,
			'side'
		);

		add_meta_box(
			'wo_moreinfo',
			__( 'Info', self::LANG ),
			array($this,'wo_info_dashboard'),
			self::LANG,
			'side'
		);
	}

	//pagination
	public function wo_slider_pagination($post)
	{
		$choosen = isset($this->aOptions['pagination']) && !empty($this->aOptions['pagination']) ? $this->aOptions['pagination'] : false;
		?>
		<input type="checkbox" name="tunna_slider[pagination]" value="1" <?php checked($choosen, 1) ?>>
		<label for="wo-pagination">Pagination</label>	
		<?php 
	}

	public function wo_info_dashboard($post)
	{
		?>
		<p>
		If you want when users click on <code>Link title</code> it scroll down to the equivalent section, please enter <code>slug</code>
		of this section.
		</p>
		<h4>Section Name - Slug</h4>
		<ol>
			<li>Home 		- #home</li>
			<li>Ourservices - #services</li>
			<li>About us 	- #aboutus</li>
			<li>Our Team 	- #ourteam</li>
			<li>Our work 	- #ourworks</li>
			<li>Our Client 	- #ourclients</li>
			<li>Contact 	- #contact</li>
		</ol>
		<?php 
	}


	//effect
	public function wo_slider_effect($post)
	{
		$choosen = isset($this->aOptions['effect']) && !empty($this->aOptions['effect']) ? $this->aOptions['effect'] : 'slide';
		$aSettings = array('slide', 'fade');
		?>
		<select name="tunna_slider[effect]">
			<?php 
				foreach ($aSettings as $key => $value)  :
					$choosen = $value == $choosen ? 'selected' : '';
					?>
					<option value="<?php echo $value ?>" <?php echo $choosen ?>><?php echo ucfirst($value) ?></option>
					<?php 
				endforeach;
			?>
		</select>
		<?php
	}

	// color schemes
	public function wo_color_schemes_dashboard($post)
	{

		$highlight = isset($this->aOptions['highlight']) ? ltrim($this->aOptions['highlight'])  : "blue";
		$aSettings = array("red", "green", "blue", "orange", "purple", "transparent");  
		?>
	 	<ul class="skin-color tunna-design">
             <?php 
            	foreach ($aSettings as $v) :
            		$active = ($v == $highlight) ? " skin-active" : '';
            		?>
        		 	<li class="<?php echo $v .  $active; ?>" data-key="<?php echo $v ?>"><a href="#"><i class="fa fa-check"></i></a></li>
            		<?php 
            	endforeach;
            ?>
     	</ul>
      	<input class="hidden" name="tunna_slider[highlight]" value="<?php echo $highlight ?>">
		<?php 
	}

	// pattern
	public function wo_pattern_dashboard($post)
	{
		
		$pattern = isset($this->aOptions['pattern']) ? $this->aOptions['pattern']  : "pattern_2";
		$aSettings = array("pattern_1", "pattern_2", "pattern_3", "pattern_4", "pattern_5", "pattern_6"); 
		?>
		<ul class="skin-color tunna-design header-patterns">
            <?php 
            	foreach ($aSettings as $v) :
            		// $checked = "";
            		$checked = $v == $pattern ? ' skin-active' : '';
            		?>
            		 <li class="<?php echo $v . $checked; ?>" data-key="<?php echo $v ?>"><a href="#"><i class="fa fa-check"></i></a></li>
            		<?php 
            	endforeach;
            ?>
         </ul>
         <input class="hidden" name="tunna_slider[pattern]" value="<?php echo $pattern ?>">
		<?php 
	}

	// overlay
	public function wo_overlay_dashboard($post)
	{
		// $aOptions = get_post_meta($post->ID, "tunna_slider", true);
		$overlay = isset($this->aOptions['overlay']) ? $this->aOptions['overlay']  : "blue"; 
		$aSettings = array("red", "green", "blue", "orange", "purple", "yellow");  
		?>
	 	<ul class="skin-color tunna-design">
             <?php 
            	foreach ($aSettings as $v) :
            		// $checked = "";
            		$checked = $v == $overlay ? ' skin-active' : '';
            		?>
            		 <li class="<?php echo $v . $checked; ?>" data-key="<?php echo $v ?>"><a href="#"><i class="fa fa-check"></i></a></li>
            		<?php 
            	endforeach;
            ?>
     	</ul>
	      <input class="hidden" name="tunna_slider[overlay]" value="<?php echo $overlay ?>">
		<?php 
	}



	// shortcode
	public function wo_tunna_shortcode($atts)
	{
		$atts = shortcode_atts( array(
				'id' => '',

			), $atts );

		extract($atts);

		if ( empty($id) ) return "Please create a slider";

		$aData = get_post_meta($id, "tunna_slider", true);
		$effect = isset($aData['effect']) && !empty($aData['effect']) ? $aData['effect'] : 'fade';
	
	

		$slide = "";
		$slide .= '<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 noPadd slides-container" style="height:100%">';


		foreach ($aData['settings']['img_url'] as $k => $v) :
		if ( !empty($v) ) :
			$slide .='<div class="slide">';
				$slide .='<div class="bannerText container textEffect">';
					$title = ( isset($aData['settings']['title'][$k]) && !empty($aData['settings']['title'][$k]) ) ? stripslashes($aData['settings']['title'][$k]) : "";
					$highline = ( isset($aData['settings']['span'][$k]) && !empty($aData['settings']['span'][$k]) ) ? stripslashes($aData['settings']['span'][$k]) : "";

					if ( !empty($title) && !empty($highline) ) : 
						$slide .= '<h1>' . stripslashes($title) . '<span>' . stripslashes($highline) . '</span></h1>';
					elseif ( !empty($title) && empty($highline) ) :
						$slide .= '<h1>' . stripslashes($title) . '</h1>';
					elseif ( empty($title) && !empty($highline) ) :
						$slide .= '<span>' . stripslashes($highline) . '</span>';	
					endif;

					
					$slide .= isset($aData['settings']['description'][$k]) && !empty($aData['settings']['description'][$k])  ? '<p>' . stripslashes($aData['settings']['description'][$k]) . '</p>' : "";
					$linkTitle = isset($aData['settings']['link_title'][$k]) && !empty($aData['settings']['link_title'][$k]) ? stripslashes($aData['settings']['link_title'][$k]) : "";
					$href = isset($aData['settings']['link_url'][$k]) && !empty($aData['settings']['link_url'][$k]) ? $aData['settings']['link_url'][$k] : '#';
					
					if ( !empty($href) ) :
						if ( !empty($linkTitle) ) :
							$slide .= '<a class="mbnt" href="'.$href.'">'.$linkTitle.'</a>';
						else :
							$slide .= '<a class="mbnt" href="'.$href.'">No Title</a>';
						endif;
					endif;

					
				$slide .='</div>';
				$slide .='<img  src="'.$v.'">';

			$slide .='</div>';
		endif;
	
		endforeach;
		$slide .= '</div>';

		$slide .= '<script type="text/javascript">';

		    $slide .= "jQuery('#banner').superslides({
		        animation: '".$effect."',
		        play: 5000
		    });";
			
			// $slide .= "jQuery('.banner').css({'background-color', '".$colorOverlay." !important'});";

		$slide .= '</script>';

		

		return $slide;
	}


	//save 
	public function tn_save_tunnar_slider($post_id)
	{
		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
		if (get_post_type($post_id) !== self::LANG ) return;

		if (!isset($_POST['post_type']) || self::LANG !== $_POST['post_type']) return;

		$settings = (isset($_POST['tunna_slider'])) ? $_POST['tunna_slider'] : array();

	

		update_post_meta($post_id, "tunna_slider",$settings);
	}

	// ajax
	public function tn_create_form_slide()
	{
		sleep(1);
		$res = '<div class="accordion">
					<div class="accordion-wrap">
						<h3 class="accordion-toggle">
							<a class="right wo-delete-image" title="Delete layer" href="#">
								<i class="fa fa-times"></i>
							</a>
							<span>New Slide</span>
						</h3>
						<div class="accordion-content">
							<div class="container-12-fluid">
								<div class="large-6">
									<div class="image-wrap">
										<img src="'.plugin_dir_url(__FILE__) .'assets/images/no-img.jpg">
									</div>
									<br>
									<a class="btn btn-white upload-img" data-insertlink=".wo-insert-link" data-append=".image-wrap">Get image</a>
									<input type="hidden" name="tunna_slider[settings][img_url][]" class="wo-insert-link" value="">
								</div>
								<div class="large-6">
									<div class="editer">
										<div class="editor-wrap">

											<div class="edit-wrap">
												<h4 class="editer-title box-first">Caption</h4>
												<div class="editer-content">
													<div class="form-group">
								                        <label class="form-label">Tittle</label>
								                        <div class="controls">
								                          <input type="text" value="" name="tunna_slider[settings][title][]" class="form-control">
								                        </div>
								                    </div>
								                    <div class="form-group">
								                        <label class="form-label">High light</label>
								                        <div class="controls">
								                          <input type="text" name="tunna_slider[settings][span][]" value="" class="form-control">
								                        </div>
								                    </div>
								                    <div class="form-group">
								                        <label class="form-label">Description</label>
								                        <div class="controls">
								                          <textarea name="tunna_slider[settings][description][]"></textarea>
								                        </div>
								                    </div>
												</div>
											</div> 
											<div class="edit-wrap">
												<h4 class="editer-title box-last">Link</h4>
												<div class="editer-content">
													<div class="form-group">
								                        <label class="form-label">Link tittle</label>
								                        <div class="controls">
								                           <input type="text" name="tunna_slider[settings][link_title][]" value="" class="form-control">
								                        </div>
								                    </div>
								                    <div class="form-group">
								                        <label class="form-label">Link url</label>
								                        <div class="controls">
								                          <input type="text" name="tunna_slider[settings][link_url][]" value="" class="form-control">
								                        </div>
								                    </div>
												</div>
											</div> 
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>';
		echo $res;
		die();
	}


	//add menu
	public function tn_add_menu()
	{
		add_submenu_page('edit.php?post_type=tunna_slider', 'Settings', 'Tunna Slider', 'manage_options', 'tunna-slider-settings', array($this, 'tn_tunnar_slider_settings'));
	}



	// register post type
	public function tn_register_post_type()
	{
		$labels = array(
						'name'			=>'Tunna Slides', 
						'singular_name'	=>'Tunna Slide',
						'menu_name'	 	=>'Tunna Slider',
						'add_new'		=>'Create Slider',
						'add_new_item'	=> 'Add New Slideshow'
					);
		$args 	= array(
						'labels'				=> $labels,
						'public'				=> true,
						'publicly_queryable'	=> true,
						'rewrite'				=> array('slug'=>'tunna-slider'),
						'supports'				=> array('title'),
						'has_archive'			=> true,
						'hierarchical'			=> false,
						'capability_type'		=> 'post',
						'menu_icon'				=> 'dashicons-images-alt2'
			);
		register_post_type( 'tunna_slider', $args );
	}

	// public function wo_hn_slider()
	// {
	// 	$menuslider = add_menu_page('Wo Slider', 'Wo Slider', 'manage_options', 'wo-slider', array($this, 'wo_admin_slider_building'));
		
	// 	add_action('admin_print_styles-'.$menuslider, array($this, 'wo_slider_include_css'));
	// 	add_action('admin_print_scripts-'.$menuslider, array($this, 'wo_slider_include_js'));
	// }

	public function tn_tunnar_slider_settings()
	{
		global $post;

		$screen = get_current_screen();

		if ($screen->post_type != self::LANG) return;

		$this->getOptions();
		include ('hnslider_settings.php');
	}

	public function tn_slider_include_scripts()
	{
		// wp_enqueue_script('jquery');
		// wp_enqueue_script('jquery-ui-widget');
		// wp_enqueue_script('jquery-ui-draggable');
		// wp_enqueue_script('jquery-ui-sortable');
		// wp_enqueue_script('jquery-ui-tabs');
		// wp_enqueue_script('jquery-ui-accordion');

		// wp_register_script('wo-jquery-custom', WO_ADMIN_JS . 'jquery.custom.js', array(), '1.0');

		// wp_enqueue_script('wo-jquery-custom');
		$screen = get_current_screen();

		if ($screen->post_type == self::LANG) :
			wp_register_style('wo-slider-font', 'https://www.google.com/fonts#ChoosePlace:select/Collection:Open+Sans:400,600,700,800,300italic,400italic,300');
	        wp_register_style('wo-slider-grid', TN_ADMIN_SLIDER_CSS . 'grid.css');
	    	wp_register_style('wo-slider-cssui', TN_ADMIN_SLIDER_CSS . 'jquery-ui-1.10.4.custom.min.css');
	    	wp_register_style('wo-slider-awe', TN_ADMIN_SLIDER_CSS . 'font-awesome.min.css');
	    	wp_register_style('wo-slider-style', TN_ADMIN_SLIDER_CSS . 'style.css');

	        wp_enqueue_style('wo-slider-font');
	        wp_enqueue_style('wo-slider-grid');
	    	wp_enqueue_style('wo-slider-cssui');
	    	wp_enqueue_style('wo-slider-awe');
	    	wp_enqueue_style('wo-slider-style');

			
	    	wp_register_script('wo-jquery-uijs', TN_ADMIN_SLIDER_JS . 'jquery-ui-1.10.4.custom.min.js');
	    	wp_register_script('wo-slider-js', TN_ADMIN_SLIDER_JS . 'jquery.custom.js');

	    	wp_enqueue_script('jquery');
	    	wp_enqueue_media();
	    	wp_enqueue_script('media-upload');
	    	wp_enqueue_script('wo-jquery-uijs');
	    	wp_enqueue_script('wo-slider-js');

	    	wp_localize_script('wo-slider-js', 'WO_AJAX', array( 'ajax_url'=>admin_url('admin-ajax.php') ));

    	endif;

	}

	public function tn_slider_include_css()
	{
		// wp_register_style('wo-slider-style', WO_ADMIN_CSS . 'style.css', array(), '1.0');

		// wp_enqueue_style('wo-slider-style');


		
	}


}

$init = new woSlider();
